package com.bosssoft.hr.train.j2se.collection;
import com.oracle.jrockit.jfr.Producer;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;


/**
 * 框架作业三：线程
 *生产者消费者，假设有个传送带，生产者往传送带上放产品，消费者从传送带上取产品
 * @author 史旭升
 * @date 2022-12-10 14:20
 */
public class ThreadArrayListDemo {
    private static int MAX_SIZE = 2;  //传送带上最多能放产品数
    private List<String> products = null; //传送带
    private ProducerTask pTask; //生产者任务
    private ConsumerTask cTask; //消费者任务

    public ThreadArrayListDemo() {
        products = new ArrayList<String>();
        pTask = new ProducerTask(products);
        cTask = new ConsumerTask(products);

        //生产者线程开启
        Thread pThread = new Thread(pTask);
        pThread.start();
        //消费者县城开启
        Thread cThread = new Thread(cTask);
        cThread.start();

        try {
            Thread.currentThread().join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

    /**
     *main方法
     * @author shi xusheng
     * @date 2022/12/11 10:52
     * @param args
     * @return void
     */
    public static void main(String[] args) {

       ThreadArrayListDemo threadArrayListDemo = new ThreadArrayListDemo();
    }

    //生产者线程
    class ProducerTask implements Runnable {

        private List<String> products;

        public ProducerTask( List<String> products) {
            this.products = products;
        }

        @Override
        public void run() {
            synchronized(products) {
                while (true) {
                    try {
                        while (products.size() >= MAX_SIZE) {
                            products.wait();
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    while (products.size() < MAX_SIZE) {
                        products.add("史旭升"+1);
                        System.out.println("生产者添加一个元素");
                        try {
                            TimeUnit.SECONDS.sleep(1);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        //唤醒
                        products.notify();
                    }
                }
            }

        }

    }

    //消费者线程
    class ConsumerTask implements Runnable {
        private List<String> products;

        public ConsumerTask(List<String> products) {
            this.products = products;
        }

        @Override
        public void run() {
            synchronized(products) {
                while (true) {
                    try {
                        while (products.size() < MAX_SIZE) {
                            products.wait();
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    while (products.size() > 0) {
                        products.remove(0);
                        System.out.println("消费者移除一个元素");
                        try {
                            TimeUnit.SECONDS.sleep(1);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        products.notify();
                    }
                }
            }
        }
    }


}

